import 'package:flutter/material.dart';
import 'package:flutter_demo/douban/widgets/star_rating.dart';

import '../../../icons/icon_font.dart';
import '../../../widgets/dash_line.dart';
import '../models/MovieItem.dart';

class HomeMovieWidget extends StatelessWidget {
  final MovieItem? movie;

  const HomeMovieWidget({Key? key, this.movie}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(8),
      decoration: const BoxDecoration(
          border: Border(bottom: BorderSide(width: 5, color: Color(0xffe2e2e2)))),
      child: Column(
        children: [
          _buildHeader(),
          const SizedBox(height: 8),
          _buildBody(),
          const SizedBox(height: 8),
          _buildFooter(),
        ],
      ),
    );
  }

  /// 头部的 排名
  Widget _buildHeader() {
    return Row(
      children: [
        Container(
            padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
            decoration: BoxDecoration(
                color: const Color.fromARGB(255, 238, 205, 144),
                borderRadius: BorderRadius.circular(3)),
            child: Text(
              "NO.${movie?.rank}",
              style: const TextStyle(fontSize: 16, color: Color.fromARGB(255, 131, 95, 36)),
            )),
      ],
    );
  }

  /// 中部的内容
  Widget _buildBody() {
    return Container(
      // height: 150,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildBodyImg(),
          _buildBodyMovieInfo(),
          SizedBox(
              height: 120,
              width: 10,
              child: DashedLine(
                  axis: Axis.vertical,
                  color: const Color.fromARGB(255, 235, 170, 60),
                  dashedHeight: 6,
                  dashedWidth: .7,
                  count: 10)),
          _buildWanted(),
        ],
      ),
    );
  }

  /// 图片
  Widget _buildBodyImg() {
    return Container(
      padding: const EdgeInsets.only(right: 8),
      child: ClipRRect(
        child: Image.network(
          "${movie?.imageURL}",
          width: 100,
          errorBuilder: (a, b, c) => Container(
              decoration: const BoxDecoration(color: Colors.lightGreen),
              height: 150,
              width: 100,
              child: const Center(child: Text("未获取到图片", style: TextStyle(color: Colors.white)))),
        ),
        borderRadius: BorderRadius.circular(5),
      ),
    );
  }

  /// 电影信息
  Widget _buildBodyMovieInfo() {
    final genres = movie?.genres?.join(" ");
    final director = movie?.director?.name;
    final actors = movie?.casts?.map((e) => e.name).join(" ");
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text.rich(TextSpan(children: [
            const WidgetSpan(
                child: Icon(Icons.play_circle_outline, color: Colors.redAccent, size: 20)),
            TextSpan(
                text: "${movie?.title}",
                style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, height: 1.2)),
            TextSpan(
                text: " (${movie?.playDate})",
                style: const TextStyle(fontSize: 16, color: Colors.black45))
          ])),
          const SizedBox(height: 8),
          FittedBox(
            child: Row(children: [
              StarRating(movie?.rating as double, size: 20),
              const SizedBox(width: 8),
              Text("${movie?.rating}")
            ]),
          ),
          const SizedBox(height: 8),
          Text("$genres / $director / $actors", maxLines: 2, overflow: TextOverflow.ellipsis)
        ],
      ),
    );
  }

  /// 想看的部分
  Widget _buildWanted() {
    return Container(
        width: 50,
        height: 100,
        padding: EdgeInsets.only(top: 20),
        child: Column(children: [
          Icon(IconFont.map['want'], size: 30, color: const Color.fromARGB(255, 235, 170, 60)),
          const Text("想看", style: TextStyle(fontSize: 16, color: Color.fromARGB(255, 235, 170, 60)))
        ]));
  }

  /// 底部
  Widget _buildFooter() {
    return Container(
      padding: EdgeInsets.all(8),
      width: double.infinity,
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(6), color: Color(0xffe2e2e2)),
      child: Text("${movie?.originalTitle}", style: TextStyle(fontSize: 16)),
    );
  }
}
